<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>用户管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
				layui.use(['laypage','layedit', 'form'], function(){
					form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
					form.render();
				});
				
				var data = ${fns:toJson(user)};
				if(!data||!data.isNewRecord){
					var internetStr = data.roleNames;
					if(internetStr){
						var internetArr = internetStr.split(",");
						for(var i=0;i<internetArr.length;i++){
							$('input[title='+internetArr[i]+']').attr("checked",true);
						}
						form.render("checkbox")
					}
				}
			$("#inputForm").validate({
				rules: {
					loginName: {remote: "${ctx}/sys/user/checkLoginName?oldLoginName=" + encodeURIComponent('${user.loginName}')}
				},
				messages: {
					loginName: {remote: "用户登录名已存在"},
					confirmNewPassword: {equalTo: "输入与上面相同的密码"}
				},
				submitHandler: function(form){
					$('#btnSubmit').blur();//去除提交按钮焦点  防止提示错误点击空格 及回车 后继续提交
					//选择是否允许登录
					if($("#user_loginFlag").val() == null || $("#user_loginFlag").val() == "" || $("#user_loginFlag").val() == undefined){
						 layer.alert('请选择是否允许登录！', {icon: 0});
						 return;
					}
					//电话号验证
					function checkContactNumber(number) {  
				        var isPhone = /^0\d{2,3}-?\d{7,8}$/;
				        var isMobile =/^(((1[3-9]{1}[0-9]{1}))+\d{8})$/;  
				        //如果为1开头则验证手机号码  
				        if (number.substring(0, 1) == 1) {  
				            if (!isMobile.exec(number)) {  
				                return false;  
				            }  
				        }  else if (number.substring(0, 1) == 0) {  
				            if (!isPhone.test(number)) {   
				                return false;  
				            }  
				        }  else {  
				            return false;  
				        } 
				        return true;  
					};
					if($("#phone").val() != "" && !checkContactNumber($("#phone").val())){
                    	layer.alert("联系电话输入错误，请正确填写！", {icon: 0});
                   		 return false;
               		 }
					if($("#mobile").val() != "" && !checkContactNumber($("#mobile").val())){
                    	layer.alert("手机号输入错误，请正确填写！", {icon: 0});
                   		 return false;
               		 }
					//选择用户角色
					var isChecked = false;
					var roles = "";
					$("input[name='roleIdList']").each(
						
					 	function(){
							if($(this).get(0).checked){
								roles += $(this).val() + ",";
							  isChecked = true;
							}
						});
					if(!isChecked){
						 layer.alert('请选择用户角色！', {icon: 0});
						 return;
					}else{
						$("#role").val(roles);
					}
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		 
	</script>
</head>
<body>
	<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
	<ul class="layui-tab-title">
		<li><a href="${ctx}/sys/user/list">用户列表</a></li>
		<li class="layui-this"><a href="${ctx}/sys/user/form?id=${user.id}">用户<shiro:hasPermission name="sys:user:edit">${not empty user.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="sys:user:edit">查看</shiro:lacksPermission></a></li>
	</ul>
	<div class="layui-tab-content"></div>
	</div><br/>
	<form:form id="inputForm" modelAttribute="user" action="${ctx}/sys/user/save" method="post" class="layui-form">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>
		<%-- 
		<div class="layui-form-item">
			<div class="layui-inline">
			<label class="layui-form-label">头像:</label>
			<div class="layui-input-inline">
				<form:hidden id="nameImage" path="photo" htmlEscape="false" maxlength="255" class="input-xlarge"/>
				<sys:ckfinder input="nameImage" type="images" uploadPath="/photo" selectMultiple="false" maxWidth="100" maxHeight="100"/>
			</div>
			</div>
		</div>
		--%>
		<div class="layui-form-item">
			<div class="layui-inline">
			<label class="layui-form-label">归属系统:</label>
			<div class="layui-input-inline">
                <sys:treeselect id="company" name="company.id" value="${user.company.id}" labelName="company.name" labelValue="${user.company.name}"
					title="公司" url="/sys/office/treeData?type=1" cssClass="required layui-input"/>
			</div>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-inline">
			<label class="layui-form-label">归属用户组:</label>
			<div class="layui-input-inline">
                <sys:treeselect id="office" name="office.id" value="${user.office.id}" labelName="office.name" labelValue="${user.office.name}"
					title="部门" url="/sys/office/treeData?type=2" cssClass="required layui-input" notAllowSelectParent="true"/>
			</div>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-inline">
			<label class="layui-form-label">姓名:</label>
			<div class="layui-input-inline">
				<form:input path="name" htmlEscape="false" maxlength="50" class="required layui-input"/>
 			</div>
				<span class="layui-form-mid"><font color="red">*</font> </span>
 			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-inline">
			<label class="layui-form-label">登录名:</label>
			<div class="layui-input-inline">
				<input id="oldLoginName" name="oldLoginName" type="hidden" value="${user.loginName}" >
				<form:input path="loginName" htmlEscape="false" maxlength="50" class="layui-input required userName"/>
			</div>
				<span class="layui-form-mid"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-inline">
			<label class="layui-form-label">密码:</label>
			<div class="layui-input-inline">
				<input id="newPassword" name="newPassword" type="password" value="" maxlength="50" minlength="3" class="layui-input ${empty user.id?'required':''}"/>
			</div>
				<c:if test="${empty user.id}">
		 		<span class="layui-form-mid"><font color="red">*</font> </span></c:if>
				<c:if test="${not empty user.id}"><span class="help-inline">若不修改密码，请留空。</span></c:if>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-inline">
			<label class="layui-form-label">确认密码:</label>
			<div class="layui-input-inline">
				<input id="confirmNewPassword" name="confirmNewPassword" type="password" value="" maxlength="50" minlength="3" equalTo="#newPassword" class="layui-input"/>
			</div>
				<c:if test="${empty user.id}"><span class="layui-form-mid"><font color="red">*</font> </span></c:if>
			
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-inline">
			<label class="layui-form-label">邮箱:</label>
			<div class="layui-input-inline">
				<form:input path="email" htmlEscape="false" maxlength="100" class="layui-input" lay-verify="email"/>
			</div>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-inline">
			<label class="layui-form-label">电话:</label>
			<div class="layui-input-inline">
				<form:input path="phone" id="phone" htmlEscape="false" maxlength="100" class="layui-input" />
			</div>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-inline">
			<label class="layui-form-label">手机:</label>
			<div class="layui-input-inline">
				<form:input path="mobile" id= "mobile" htmlEscape="false" maxlength="100" class="layui-input" />
			</div>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">是否允许登录:</label>
			<div class="layui-input-inline">
				<select id="user_loginFlag" name="loginFlag" data-value="" lay-verify="required" class="required">
			 	<option value=""  ${user.loginFlag==''?'selected':''}></option>
	                    <c:forEach items="${fns:getDictList('yes_no')}" var="dict">
	                        <option value="${dict.value}" ${dict.value==user.loginFlag?'selected':''} >${dict.label}</option>
	                    </c:forEach>
				 </select>
			</div>
				<span class="layui-form-mid"><font color="red">* “是”代表此账号允许登录，“否”则表示此账号不允许登录</font> </span>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">用户类型:</label>
			<div class="layui-input-inline">
				<select id="user_userType" name="userType" data-value="">
					<option value="" ${user.userType==''?'selected':''}></option>
					<c:forEach items="${fns:getDictList('sys_user_type')}" var="dict">
					 	<option value="${dict.value}" ${dict.value==user.userType?'selected':''}>${dict.label}</option>
					 </c:forEach>
				</select>
			</div>
		</div>
		<div class="layui-form-item">
		 	<label class="layui-form-label">用户角色:</label>
			<div class="layui-input-block">
			<input type="hidden" id="roleList" >
				<c:forEach items="${allRoles}" var="dict" varStatus="dictStatus">
	                <span><input id="rollList${dictStatus.index}" name="roleIdList" type="checkbox" value="${dict.id}"  data-value="" title="${dict.name}" lay-verify="required"></span>
	            </c:forEach><span class="layui-form-right"><font color="red">* 注：此处为必选项</font></span>&nbsp;
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-inline">
			<label class="layui-form-label">备注:</label>
			<div class="layui-input-inline">
				<form:textarea path="remarks" htmlEscape="false" rows="3" maxlength="200" class="layui-textarea"/>
			</div>
			</div>
		</div>
		<c:if test="${not empty user.id}">
			<div class="control-group">
				<label class="control-label">创建时间:</label>
				<div class="controls">
					<label class="lbl"><fmt:formatDate value="${user.createDate}" type="both" dateStyle="full"/></label>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">最后登陆:</label>
				<div class="controls">
					<label class="lbl">IP: ${user.loginIp}&nbsp;&nbsp;&nbsp;&nbsp;时间：<fmt:formatDate value="${user.loginDate}" type="both" dateStyle="full"/></label>
				</div>
			</div>
		</c:if>
		<div class="layui-form-item">
			<div class="layui-input-block">
			<shiro:hasPermission name="sys:user:edit"><input id="btnSubmit" class="layui-btn" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="layui-btn layui-btn-primary" type="button" value="返 回" onclick="history.go(-1)"/>
			</div>
		</div>
	</form:form>
</body>
</html>